package com.ytzl.itrip.service.impl;

import com.ytzl.itrip.common.BigDecimalUtil;
import com.ytzl.itrip.dao.ItripCommentMapper;
import com.ytzl.itrip.dao.ItripHotelOrderMapper;
import com.ytzl.itrip.dao.ItripImageMapper;
import com.ytzl.itrip.pojo.ItripImage;
import com.ytzl.itrip.service.ItripCommentService;
import com.ytzl.itrip.pojo.ItripComment;
import com.ytzl.itrip.common.EmptyUtils;
import com.ytzl.itrip.common.Constants;
import com.ytzl.itrip.common.Page;
import com.ytzl.itrip.service.ItripImageService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ItripCommentServiceImpl implements ItripCommentService {

    @Resource
    private ItripCommentMapper itripCommentMapper;

    @Resource
    private ItripImageMapper itripImageMapper;

    @Resource
    private ItripHotelOrderMapper itripHotelOrderMapper;

    public ItripComment getItripCommentById(Long id) throws Exception {
        return itripCommentMapper.getItripCommentById(id);
    }

    public Integer removeItripCommentById(Long id) throws Exception {
        return itripCommentMapper.removeItripCommentById(id);
    }

    public List<ItripComment> getItripCommentListByMap(Map
                                                               <String, Object> param) throws Exception {
        List<ItripComment> list = itripCommentMapper.getItripCommentListByMap(param);
        return list;
    }

    public Integer getItripCommentCountByMap(Map
                                                     <String, Object> param) throws Exception {
        return itripCommentMapper.getItripCommentCountByMap(param);
    }

    public Integer saveItripComment(ItripComment itripComment) throws Exception {
        itripComment.setCreationDate(new Date());
        return itripCommentMapper.saveItripComment(itripComment);
    }

    public Integer modifyItripComment(ItripComment itripComment) throws Exception {
        itripComment.setModifyDate(new Date());
        return itripCommentMapper.modifyItripComment(itripComment);
    }

    public Integer removeItripCommentByIds(Long[] ids) throws Exception {
        return itripCommentMapper.removeItripCommentByIds(ids);
    }

    @Override
    public void saveItripCommentAndItripImage(ItripComment itripComment, List<ItripImage> itripImages) throws Exception {
        //计算评分
        int sum = itripComment.getFacilitiesScore() + itripComment.getHygieneScore() + itripComment.getPositionScore() + itripComment.getServiceScore();
        //BigDecimal.ROUND_DOWN 接近零取整的方式
        float source = BigDecimalUtil.OperationASMD(sum, 4, BigDecimalUtil.BigDecimalOprations.divide, 1, BigDecimal.ROUND_DOWN).floatValue();
        //四舍五入
        itripComment.setScore(Math.round(source));
        //保存评论信息
        if (itripCommentMapper.saveItripComment(itripComment) > 0) {
            System.out.print("添加评论成功--id=" + itripComment.getId());
            //保存评论图片地址
            for (ItripImage itripImage : itripImages) {
                itripImage.setTargetId(itripComment.getId());
                itripImageMapper.saveItripImage(itripImage);
            }
            //更新订单状态为 4
            itripHotelOrderMapper.modifyHotelOrderStatus(itripComment.getOrderId(), itripComment.getCreatedBy());
        }
    }

    public Page<ItripComment> queryItripCommentPageByMap(Map<String, Object> param, Integer pageNo, Integer pageSize) throws Exception {
        Integer total = itripCommentMapper.getItripCommentCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<ItripComment> itripCommentList = itripCommentMapper.getItripCommentListByMap(param);
        page.setRows(itripCommentList);
        return page;
    }

}
